package com.caac.core.util;

import java.io.Serializable;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2006</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public class Page implements Serializable {
    public static final int DEFAULT_PAGE_NO = 1;
    public static final int DEFAULT_PAGE_SIZE = 15;
    public static final int DEFAULT_LINK_PAGE_COUNT = 15;

    private int rowCount;
    private int pageNo;
    private int pageSize;
    private int linkPageCount;

    private int firstPageNo;
    private int lastPageNo;
    private int rowFrom;
    private int rowTo;

    public Page(int rowCount, int pageNo, int pageSize, int linkPageCount) {
        if (rowCount < 0) {
            this.rowCount = 0;
        } else {
            this.rowCount = rowCount;
        }

        if (pageSize < 1) {
            this.pageSize = DEFAULT_PAGE_SIZE;
        } else {
            this.pageSize = pageSize;
        }

        if (linkPageCount < 1) {
            this.linkPageCount = DEFAULT_LINK_PAGE_COUNT;
        } else {
            this.linkPageCount = linkPageCount;
        }

        firstPageNo = 1;
        lastPageNo = (rowCount - 1) / pageSize + 1;

        if (pageNo < firstPageNo) {
            this.pageNo = firstPageNo;
        } else if (pageNo > lastPageNo) {
            this.pageNo = lastPageNo;
        } else {
            this.pageNo = pageNo;
        }

        this.rowFrom = (this.pageNo - 1) * this.pageSize + 1;
        this.rowTo = this.pageNo * this.pageSize > rowCount ? rowCount :
                     this.pageNo * this.pageSize;
    }

    public Page(int rowCount, int pageNo, int pageSize) {
        this(rowCount, pageNo, pageSize, DEFAULT_LINK_PAGE_COUNT);
    }

    public Page(int rowCount, int pageNo) {
        this(rowCount, pageNo, DEFAULT_PAGE_SIZE, DEFAULT_LINK_PAGE_COUNT);
    }

    public int getRowCount() {
        return rowCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getLinkPageCount() {
        return linkPageCount;
    }

    public int getPageNo() {
        return pageNo;
    }

    public int getFirstPageNo() {
        return firstPageNo;
    }

    public int getLastPageNo() {
        return lastPageNo;
    }

    public int getRowFrom() {
        return rowFrom;
    }

    public int getRowTo() {
        return rowTo;
    }

    public int getPreviousPageNo() {
        if (!isHasPreviousPage()) {
            return firstPageNo;
        }
        return pageNo - 1;
    }

    public int getNextPageNo() {
        if (!isHasNextPage()) {
            return lastPageNo;
        }
        return pageNo + 1;
    }

    public boolean isHasNextPage() {
        return pageNo < lastPageNo;
    }

    public boolean isHasPreviousPage() {
        return pageNo > firstPageNo;
    }
}
